package net.zjitc.controller;


import net.zjitc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@RequestMapping("/user")
@Controller
public class UserController {

    //model是从容器中获得的
    //public String login(@RequestParam("uname") String username, String password,int id, Model model)
    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, Model model) {
        String username = request.getParameter("uname");
        String password = request.getParameter("password");
        if ("zhangsan".equals(username) && "123456".equals(password)) {
            //登录成功后，用户名放在session中，以便index或其它页面从session中取
            session.setAttribute("username", username);
            return "index";
        } else {
            //model会将内容放入request域中
            model.addAttribute("message", "用户名或密码错误");
            //request.setAttribute("message","用户名或密码错误");
            return "login";
        }
    }
    //1、ModelAndView:
    @PostMapping("/login2")
    public ModelAndView login2(String uname,String password) {
        //假设登录失败
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        modelAndView.addObject("message","用户名或密码错误");
        //modelAndView.addObject("user",new User())
        return modelAndView;
    }
    //两种：请求转发，重定向：如id=1，请求转发，转发到login.jsp;id!=1,重定向：index.jsp(baidu.com)
    //login请求，
    @GetMapping("/test3")
    public String test3(String id) {
        if ("1".equals(id)){
            return "forward:login.jsp";
        }else {
            //重定向到百度
        return "redirect:index.jsp";
        }
    }

    //添加用户：注册用户
    @PostMapping("/register")
    public String register(User user,Model model){
        model.addAttribute("username",user.getUsername());
        return "success";
    }

    //添加用户：注册用户
    @PostMapping("/date")
    public String date(@DateTimeFormat(pattern = "yyyy/MM/dd") Date birthday){
       // Calendar calendar = birthday.
        System.out.println(birthday);
        return "success";
    }

    @PostMapping("/product")
    public String productAdd(int[] pname,Model model){
        System.out.println(pname);
        for (int i:pname){
            System.out.println(i);
        }
//        pname是一个数组对象，如果需要看里面的值，可通过循环打印，也可使用方法转成字符串形式
        model.addAttribute("pname", Arrays.toString(pname));
        return "success";
    }

    /**
     * 使用List集合类型的时候，需要对参数加注解绑定，
     * @param pname
     * @param model
     * @return
     */
    @PostMapping("/product2")
    public String productAdd2(@RequestParam("pname") List<Integer> pname, Model model){
        System.out.println(pname);
        for (int i:pname){
            System.out.println(i);
        }
//        pname是一个数组对象，如果需要看里面的值，可通过循环打印，也可使用方法转成字符串形式
        model.addAttribute("pname", pname.toArray());
        return "success";
    }

    @PostMapping("/order")
    public String order(User user, Model model){
        System.out.println(user.toString());
//        pname是一个数组对象，如果需要看里面的值，可通过循环打印，也可使用方法转成字符串形式
        model.addAttribute("pname",user.toString());
        return "success.jsp";
    }

}
